package com.simen.kv.sample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.simen.kv.Columns;
import com.simen.kv.KV;
import com.simen.kv.KVManager;
import com.simen.kv.Where;

import java.util.Iterator;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    public final static String KEY = "keys";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.add).setOnClickListener(this);
        findViewById(R.id.addRandom).setOnClickListener(this);
        findViewById(R.id.get).setOnClickListener(this);
        findViewById(R.id.getAll).setOnClickListener(this);
        findViewById(R.id.del).setOnClickListener(this);
        findViewById(R.id.clear).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                KVManager.getInstance().update(Constants.DB3, KEY + System.currentTimeMillis(),
                        "hellos" + System
                        .currentTimeMillis());
                break;
            case R.id.addRandom:
                KVManager.getInstance().update(Constants.DB3, KEY, "hellos" + System
                        .currentTimeMillis());
                break;
            case R.id.get:
                KV<String> kv = KVManager.getInstance().getKV(Constants.DB3, KEY);
                if (kv != null) {
                    Log.d(TAG, "get: " + kv.getKey() + " " + kv.getValue() + " " + kv.getCreate());
                }
                break;
            case R.id.getAll:
//                Where where = new Where();
//                //查询过去3分钟的记录
//                long TimeMillis = System.currentTimeMillis();
//                where.beforeCreate(Long.toString(TimeMillis), true).And().afterCreate(Long
// .toString
//                        (TimeMillis - 3 * 60 * 60 * 1000), true);
//                //指定结果按照createtime降序排列
//                where.orderBy(Where.DESC, Columns.CREATETIMENAME);
//                //从结果中最多取10个数据
//                where.sub(0, 10);
                List<KV> list = KVManager.getInstance().getKVs(Constants.DB3, null);
                Iterator<KV> iterator = list.iterator();
                while (iterator.hasNext()) {
                    KV item = iterator.next();
                    Log.d(TAG, "getALL: " + item.getId() + " " + item.getKey() + " " + item
                            .getValue());
                }
                break;
            case R.id.del:
                List<KV<String>> kvs = KVManager.getInstance().getKVs(Constants.DB3, null);
                if (kvs == null || kvs.isEmpty()) {
                    return;
                }

                KVManager.getInstance().remove(Constants.DB3, kvs.get(0).getKey());
                break;
            case R.id.clear:
                KVManager.getInstance().clear(Constants.DB3);
                break;
        }
    }
}
